const { getHero } = require('../dota2api/index')

function isWin(playSlot, radiantWin) {
  return [0, 1, 2, 3, 4].includes(playSlot) === radiantWin
}

function isLadder(lobbyType) {
  return lobbyType === 7
}

function isParty(partySize) {
  return partySize > 1
}

async function getHeroName(heroId) {
  return new Promise(async (resolve, reject) => {
    try {
      if (heroObject[heroId]) {
        resolve(heroObject[heroId])
      } else {
        const heroList = await getHero()
        const currentHero = heroList.find(item => {
          return item.id === heroId
        })
        resolve(currentHero.localized_name)
      }
    } catch (error) {
      reject(error)
    }
  })
}

function getRankTier(rankTier, leaderboard_rank = '') {
  return new Promise((resolve, reject) => {
    try {
      if (rankTier) {
        const tier = tierList[Number(rankTier.toString()[0])]
        const level = levelList[Number(rankTier.toString()[1])]
        let tierLevel
        if (Number(rankTier.toString()[0]) === 8) {
          tierLevel = `【${tier}${leaderboard_rank}】`
        } else {
          tierLevel = `【${tier}${level}】`
        }
        resolve(tierLevel)
      } else {
        resolve('未知')
      }
    } catch (error) {
      reject(error)
    }
  })
}

function getItemCnName(itemId) {
  return (itemObject[itemId.toString()]) || '未知物品'
}

function getHeroCnName(heroId) {
  return (heroObject[heroId]) || '未知英雄'
}

function getGameMode(game_mode) {
  return gameMode[game_mode] || '未知模式'
}

function getLobbyType(lobby_type) {
  return lobbyType[lobby_type] || '未知'
}

function getPartSize(party_size) {
  return partySize[party_size] || '未知'
}

function randomHero() {
  const randomNumer = randomNum(1, 135)
  if ([115, 116, 117, 118, 130, 131, 132, 133, 134].includes(randomNumer)) {
    return randomHero()
  } else {
    return heroObject[randomNumer]
  }
}

function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
      break;
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
      break;
    default:
      return 0;
      break;
  }
}

const heroObject = {
  1: '敌法师',
  2: '斧王',
  3: '痛苦之源',
  4: '血魔',
  5: '水晶室女',
  6: '卓尔游侠',
  7: '撼地者',
  8: '主宰',
  9: '米拉娜',
  10: '变体精灵',
  11: '影魔',
  12: '幻影长矛手',
  13: '帕克',
  14: '帕吉',
  15: '剃刀',
  16: '沙王',
  17: '风暴之灵',
  18: '斯温',
  19: '小小',
  20: '复仇之魂',
  21: '风行者',
  22: '宙斯',
  23: '昆卡',
  25: '丽娜',
  26: '莱恩',
  27: '暗影萨满',
  28: '斯拉达',
  29: '潮汐猎人',
  30: '巫医',
  31: '巫妖',
  32: '力丸',
  33: '谜团',
  34: '地精修补匠',
  35: '狙击手',
  36: '瘟疫法师',
  37: '术士',
  38: '兽王',
  39: '痛苦女王',
  40: '剧毒术士',
  41: '虚空假面',
  42: '骷髅王',
  43: '死亡先知',
  44: '幻影刺客',
  45: '帕格纳',
  46: '圣堂刺客',
  47: '毒龙',
  48: '露娜',
  49: '龙骑士',
  50: '戴泽',
  51: '发条地精',
  52: '受折磨的灵魂',
  53: '先知',
  54: '噬魂者',
  55: '黑暗贤者',
  56: '克林克兹',
  57: '全能骑士',
  58: '魅惑魔女',
  59: '哈斯卡',
  60: '暗夜魔王',
  61: '育母蜘蛛',
  62: '赏金猎人',
  63: '编织者',
  64: '双头龙',
  65: '蝙蝠骑士',
  66: '陈',
  67: '幽鬼',
  68: '远古冰魂',
  69: '末日使者',
  70: '拍拍熊',
  71: '裂魂人',
  72: '矮人直升机',
  73: '炼金术士',
  74: '祈求者',
  75: '沉默术士',
  76: '殁境神蚀者',
  77: '狼人',
  78: '酒仙',
  79: '暗影恶魔',
  80: '德鲁伊',
  81: '混沌骑士',
  82: '米波',
  83: '树精卫士',
  84: '食人魔法师',
  85: '不朽尸王',
  86: '拉比克',
  87: '干扰者',
  88: '司夜刺客',
  89: '娜迦海妖',
  90: '光之守卫',
  91: '艾欧',
  92: '维萨吉',
  93: '斯拉克',
  94: '美杜莎',
  95: '巨魔战将',
  96: '半人马战行者',
  97: '马格纳斯',
  98: '伐木机',
  99: '钢背兽',
  100: '巨牙海民',
  101: '天怒法师',
  102: '亚巴顿',
  103: '上古巨神',
  104: '军团指挥官',
  105: '工程师',
  106: '灰烬之灵',
  107: '大地之灵',
  108: '孽主',
  109: '恐怖利刃',
  110: '凤凰',
  111: '神谕者',
  112: '寒冬飞龙',
  113: '天穹守望者',
  114: '齐天大圣',
  119: '邪影芳灵',
  120: '石鳞剑士',
  121: '天涯墨客',
  123: '森海飞霞',
  126: '虚无之灵',
  128: '电炎绝手',
  129: '马尔斯',
  135: '破晓辰星'
}

const itemObject = {
  "1": "blink",
  "2": "blades_of_attack",
  "3": "broadsword",
  "4": "chainmail",
  "5": "claymore",
  "6": "helm_of_iron_will",
  "7": "javelin",
  "8": "mithril_hammer",
  "9": "platemail",
  "10": "quarterstaff",
  "11": "quelling_blade",
  "12": "ring_of_protection",
  "13": "gauntlets",
  "14": "slippers",
  "15": "mantle",
  "16": "branches",
  "17": "belt_of_strength",
  "18": "boots_of_elves",
  "19": "robe",
  "20": "circlet",
  "21": "ogre_axe",
  "22": "blade_of_alacrity",
  "23": "staff_of_wizardry",
  "24": "ultimate_orb",
  "25": "gloves",
  "26": "lifesteal",
  "27": "ring_of_regen",
  "28": "sobi_mask",
  "29": "boots",
  "30": "gem",
  "31": "cloak",
  "32": "talisman_of_evasion",
  "33": "cheese",
  "34": "magic_stick",
  "35": "recipe_magic_wand",
  "36": "magic_wand",
  "37": "ghost",
  "38": "clarity",
  "39": "flask",
  "40": "dust",
  "41": "bottle",
  "42": "ward_observer",
  "43": "ward_sentry",
  "44": "tango",
  "45": "courier",
  "46": "tpscroll",
  "47": "recipe_travel_boots",
  "48": "travel_boots",
  "49": "recipe_phase_boots",
  "50": "phase_boots",
  "51": "demon_edge",
  "52": "eagle",
  "53": "reaver",
  "54": "relic",
  "55": "hyperstone",
  "56": "ring_of_health",
  "57": "void_stone",
  "58": "mystic_staff",
  "59": "energy_booster",
  "60": "point_booster",
  "61": "vitality_booster",
  "62": "recipe_power_treads",
  "63": "power_treads",
  "64": "recipe_hand_of_midas",
  "65": "hand_of_midas",
  "66": "recipe_oblivion_staff",
  "67": "oblivion_staff",
  "68": "recipe_pers",
  "69": "pers",
  "70": "recipe_poor_mans_shield",
  "71": "poor_mans_shield",
  "72": "recipe_bracer",
  "73": "bracer",
  "74": "recipe_wraith_band",
  "75": "wraith_band",
  "76": "recipe_null_talisman",
  "77": "null_talisman",
  "78": "recipe_mekansm",
  "79": "mekansm",
  "80": "recipe_vladmir",
  "81": "vladmir",
  "85": "recipe_buckler",
  "86": "buckler",
  "87": "recipe_ring_of_basilius",
  "88": "ring_of_basilius",
  "89": "recipe_pipe",
  "90": "pipe",
  "91": "recipe_urn_of_shadows",
  "92": "urn_of_shadows",
  "93": "recipe_headdress",
  "94": "headdress",
  "95": "recipe_sheepstick",
  "96": "sheepstick",
  "97": "recipe_orchid",
  "98": "orchid",
  "99": "recipe_cyclone",
  "100": "cyclone",
  "101": "recipe_force_staff",
  "102": "force_staff",
  "103": "recipe_dagon",
  "104": "dagon",
  "105": "recipe_necronomicon",
  "106": "necronomicon",
  "107": "recipe_ultimate_scepter",
  "108": "ultimate_scepter",
  "109": "recipe_refresher",
  "110": "refresher",
  "111": "recipe_assault",
  "112": "assault",
  "113": "recipe_heart",
  "114": "heart",
  "115": "recipe_black_king_bar",
  "116": "black_king_bar",
  "117": "aegis",
  "118": "recipe_shivas_guard",
  "119": "shivas_guard",
  "120": "recipe_bloodstone",
  "121": "bloodstone",
  "122": "recipe_sphere",
  "123": "sphere",
  "124": "recipe_vanguard",
  "125": "vanguard",
  "126": "recipe_blade_mail",
  "127": "blade_mail",
  "128": "recipe_soul_booster",
  "129": "soul_booster",
  "130": "recipe_hood_of_defiance",
  "131": "hood_of_defiance",
  "132": "recipe_rapier",
  "133": "rapier",
  "134": "recipe_monkey_king_bar",
  "135": "monkey_king_bar",
  "136": "recipe_radiance",
  "137": "radiance",
  "138": "recipe_butterfly",
  "139": "butterfly",
  "140": "recipe_greater_crit",
  "141": "greater_crit",
  "142": "recipe_basher",
  "143": "basher",
  "144": "recipe_bfury",
  "145": "bfury",
  "146": "recipe_manta",
  "147": "manta",
  "148": "recipe_lesser_crit",
  "149": "lesser_crit",
  "150": "recipe_armlet",
  "151": "armlet",
  "152": "invis_sword",
  "153": "recipe_sange_and_yasha",
  "154": "sange_and_yasha",
  "155": "recipe_satanic",
  "156": "satanic",
  "157": "recipe_mjollnir",
  "158": "mjollnir",
  "159": "recipe_skadi",
  "160": "skadi",
  "161": "recipe_sange",
  "162": "sange",
  "163": "recipe_helm_of_the_dominator",
  "164": "helm_of_the_dominator",
  "165": "recipe_maelstrom",
  "166": "maelstrom",
  "167": "recipe_desolator",
  "168": "desolator",
  "169": "recipe_yasha",
  "170": "yasha",
  "171": "recipe_mask_of_madness",
  "172": "mask_of_madness",
  "173": "recipe_diffusal_blade",
  "174": "diffusal_blade",
  "175": "recipe_ethereal_blade",
  "176": "ethereal_blade",
  "177": "recipe_soul_ring",
  "178": "soul_ring",
  "179": "recipe_arcane_boots",
  "180": "arcane_boots",
  "181": "orb_of_venom",
  "182": "stout_shield",
  "183": "recipe_invis_sword",
  "184": "recipe_ancient_janggo",
  "185": "ancient_janggo",
  "186": "recipe_medallion_of_courage",
  "187": "medallion_of_courage",
  "188": "smoke_of_deceit",
  "189": "recipe_veil_of_discord",
  "190": "veil_of_discord",
  "191": "recipe_necronomicon_2",
  "192": "recipe_necronomicon_3",
  "193": "necronomicon_2",
  "194": "necronomicon_3",
  "196": "diffusal_blade_2",
  "197": "recipe_dagon_2",
  "198": "recipe_dagon_3",
  "199": "recipe_dagon_4",
  "200": "recipe_dagon_5",
  "201": "dagon_2",
  "202": "dagon_3",
  "203": "dagon_4",
  "204": "dagon_5",
  "205": "recipe_rod_of_atos",
  "206": "rod_of_atos",
  "207": "recipe_abyssal_blade",
  "208": "abyssal_blade",
  "209": "recipe_heavens_halberd",
  "210": "heavens_halberd",
  "211": "recipe_ring_of_aquila",
  "212": "ring_of_aquila",
  "213": "recipe_tranquil_boots",
  "214": "tranquil_boots",
  "215": "shadow_amulet",
  "216": "enchanted_mango",
  "217": "recipe_ward_dispenser",
  "218": "ward_dispenser",
  "219": "recipe_travel_boots_2",
  "220": "travel_boots_2",
  "221": "recipe_lotus_orb",
  "222": "recipe_meteor_hammer",
  "223": "meteor_hammer",
  "224": "recipe_nullifier",
  "225": "nullifier",
  "226": "lotus_orb",
  "227": "recipe_solar_crest",
  "228": "recipe_octarine_core",
  "229": "solar_crest",
  "230": "recipe_guardian_greaves",
  "231": "guardian_greaves",
  "232": "aether_lens",
  "233": "recipe_aether_lens",
  "234": "recipe_dragon_lance",
  "235": "octarine_core",
  "236": "dragon_lance",
  "237": "faerie_fire",
  "238": "recipe_iron_talon",
  "239": "iron_talon",
  "240": "blight_stone",
  "241": "tango_single",
  "242": "crimson_guard",
  "243": "recipe_crimson_guard",
  "244": "wind_lace",
  "245": "recipe_bloodthorn",
  "246": "recipe_moon_shard",
  "247": "moon_shard",
  "248": "recipe_silver_edge",
  "249": "silver_edge",
  "250": "bloodthorn",
  "251": "recipe_echo_sabre",
  "252": "echo_sabre",
  "253": "recipe_glimmer_cape",
  "254": "glimmer_cape",
  "255": "recipe_aeon_disk",
  "256": "aeon_disk",
  "257": "tome_of_knowledge",
  "258": "recipe_kaya",
  "259": "kaya",
  "260": "refresher_shard",
  "261": "crown",
  "262": "recipe_hurricane_pike",
  "263": "hurricane_pike",
  "265": "infused_raindrop",
  "266": "recipe_spirit_vessel",
  "267": "spirit_vessel",
  "268": "recipe_holy_locket",
  "269": "holy_locket",
  "270": "recipe_ultimate_scepter_2",
  "271": "ultimate_scepter_2",
  "272": "recipe_kaya_and_sange",
  "273": "kaya_and_sange",
  "274": "recipe_yasha_and_kaya",
  "275": "recipe_trident",
  "276": "combo_breaker",
  "277": "yasha_and_kaya",
  "279": "ring_of_tarrasque",
  "286": "flying_courier",
  "287": "keen_optic",
  "288": "grove_bow",
  "289": "quickening_charm",
  "290": "philosophers_stone",
  "291": "force_boots",
  "292": "desolator_2",
  "293": "phoenix_ash",
  "294": "seer_stone",
  "295": "greater_mango",
  "297": "vampire_fangs",
  "298": "craggy_coat",
  "299": "greater_faerie_fire",
  "300": "timeless_relic",
  "301": "mirror_shield",
  "302": "elixer",
  "303": "recipe_ironwood_tree",
  "304": "ironwood_tree",
  "305": "royal_jelly",
  "306": "pupils_gift",
  "307": "tome_of_aghanim",
  "308": "repair_kit",
  "309": "mind_breaker",
  "310": "third_eye",
  "311": "spell_prism",
  "312": "horizon",
  "313": "fusion_rune",
  "317": "recipe_fallen_sky",
  "325": "princes_knife",
  "326": "spider_legs",
  "327": "helm_of_the_undying",
  "328": "mango_tree",
  "329": "recipe_vambrace",
  "330": "witless_shako",
  "331": "vambrace",
  "334": "imp_claw",
  "335": "flicker",
  "336": "spy_gadget",
  "349": "arcane_ring",
  "354": "ocean_heart",
  "355": "broom_handle",
  "356": "trusty_shovel",
  "357": "nether_shawl",
  "358": "dragon_scale",
  "359": "essence_ring",
  "360": "clumsy_net",
  "361": "enchanted_quiver",
  "362": "ninja_gear",
  "363": "illusionsts_cape",
  "364": "havoc_hammer",
  "365": "panic_button",
  "366": "apex",
  "367": "ballista",
  "368": "woodland_striders",
  "369": "trident",
  "370": "demonicon",
  "371": "fallen_sky",
  "372": "pirate_hat",
  "373": "dimensional_doorway",
  "374": "ex_machina",
  "375": "faded_broach",
  "376": "paladin_sword",
  "377": "minotaur_horn",
  "378": "orb_of_destruction",
  "379": "the_leveller",
  "381": "titan_sliver",
  "473": "voodoo_mask",
  "485": "blitz_knuckles",
  "533": "recipe_witch_blade",
  "534": "witch_blade",
  "565": "chipped_vest",
  "566": "wizard_glass",
  "569": "orb_of_corrosion",
  "570": "gloves_of_travel",
  "571": "trickster_cloak",
  "573": "elven_tunic",
  "574": "cloak_of_flames",
  "575": "venom_gland",
  "576": "gladiator_helm",
  "577": "possessed_mask",
  "578": "ancient_perseverance",
  "582": "oakheart",
  "585": "stormcrafter",
  "588": "overflowing_elixir",
  "589": "mysterious_hat",
  "593": "fluffy_hat",
  "596": "falcon_blade",
  "597": "recipe_mage_slayer",
  "598": "mage_slayer",
  "599": "recipe_falcon_blade",
  "600": "overwhelming_blink",
  "603": "swift_blink",
  "604": "arcane_blink",
  "606": "recipe_arcane_blink",
  "607": "recipe_swift_blink",
  "608": "recipe_overwhelming_blink",
  "609": "aghanims_shard",
  "610": "wind_waker",
  "612": "recipe_wind_waker",
  "633": "recipe_helm_of_the_overlord",
  "635": "helm_of_the_overlord",
  "637": "star_mace",
  "638": "penta_edged_sword",
  "640": "recipe_orb_of_corrosion",
  "653": "recipe_grandmasters_glaive",
  "655": "grandmasters_glaive",
  "674": "warhammer",
  "675": "psychic_headband",
  "676": "ceremonial_robe",
  "677": "book_of_shadows",
  "678": "giants_ring",
  "679": "vengeances_shadow",
  "680": "bullwhip",
  "686": "quicksilver_amulet",
  "691": "recipe_eternal_shroud",
  "692": "eternal_shroud",
  "725": "aghanims_shard_roshan",
  "727": "ultimate_scepter_roshan",
  "731": "satchel",
  "824": "assassins_dagger",
  "825": "ascetic_cap",
  "826": "sample_picker",
  "827": "icarus_wings",
  "828": "misericorde",
  "829": "force_field",
  "833": "tree_processor",
  "834": "black_powder_bag",
  "835": "paintball",
  "836": "light_robes",
  "837": "heavy_blade",
  "838": "unstable_wand",
  "839": "fortitude_ring",
  "840": "pogo_stick",
  "849": "mechanical_arm",
  "1021": "river_painter",
  "1022": "river_painter2",
  "1023": "river_painter3",
  "1024": "river_painter4",
  "1025": "river_painter5",
  "1026": "river_painter6",
  "1027": "river_painter7",
  "1028": "mutation_tombstone",
  "1029": "super_blink",
  "1030": "pocket_tower",
  "1032": "pocket_roshan",
  "1466": "gungir",
  "1565": "recipe_gungir"
}

const gameMode = {
  1: '全英雄选择',
  2: '队长模式',
  3: '随机征召',
  4: '单一征召',
  18: '技能征召',
  22: '全英雄选择（BP）'
}

const lobbyType = {
  0: '普通',
  1: '练习赛',
  2: '锦标赛',
  3: '教程',
  4: '合作对抗机器人',
  5: '天体组队匹配（旧有）',
  6: '天梯单排（旧有）',
  7: '天梯',
  8: '中路对单',
  9: '勇士联赛'
}

const partySize = {
  1: '单排',
  2: '双排',
  3: '三排',
  4: '四黑',
  5: '五黑'
}

const tierList = {
  0: '未知',
  1: '卫士',
  2: '先锋',
  3: '中军',
  4: '统帅',
  5: '传奇',
  6: '万古流芳',
  7: '超凡入圣',
  8: '冠绝一世'
}

const levelList = {
  0: '',
  1: 'Ⅰ',
  2: 'Ⅱ',
  3: 'Ⅲ',
  4: 'Ⅳ',
  5: 'Ⅴ',
  6: 'Ⅵ',
  7: 'Ⅶ'
}

module.exports = {
  isWin,
  isLadder,
  isParty,
  getHeroName,
  getHeroCnName,
  getGameMode,
  getLobbyType,
  getPartSize,
  getRankTier,
  randomHero,
  getItemCnName
}